import 'package:erp/data/model/process_inform.dart';
import 'package:erp/data/model/process_node.dart';
import 'package:erp/res/colors.dart';
import 'package:erp/util/image_utils.dart';
import 'package:erp/widgets/my_app_bar.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import 'controller.dart';

export 'binding.dart';

class FlowChart extends StatelessWidget {
  ///
  _buildGraph(List<ProcessNode> nodes) {
    return nodes.map((processNode) {
      var bgProvider, boxConstraints;
      if (processNode.nodeCode == "START") {
        bgProvider = ImageUtils.getImageProvider("", holderImg: "flow_chart/start_node");
        boxConstraints = BoxConstraints(maxHeight: 90.h, maxWidth: 445.w);
      } else if (processNode.nodeCode == "END") {
        bgProvider = ImageUtils.getImageProvider("", holderImg: "flow_chart/end_node");
        boxConstraints = BoxConstraints(maxHeight: 70.h, maxWidth: 445.w);
      } else {
        bgProvider = ImageUtils.getImageProvider("", holderImg: "flow_chart/middle_node");
        boxConstraints = BoxConstraints(maxHeight: 110.h, maxWidth: 445.w);
      }
      return Container(
        constraints: boxConstraints,
        decoration: BoxDecoration(image: DecorationImage(image: bgProvider, fit: BoxFit.fitWidth)),
        child: Center(
          child: Text(
            "${processNode.nodeName}",
            style: TextStyle(color: Colors.white, fontSize: 26.sp, fontWeight: FontWeight.w400),
          ),
        ),
      );
    });
  }

  _buildInfo(List<ProcessInform> nodes) {
    nodes.map((processInform) => null);
    var style = TextStyle(color: Color(0xFFB3B3B3), fontSize: 20.sp, fontWeight: FontWeight.w400);
    return Container(
      constraints: BoxConstraints(maxWidth: 500.w, minHeight: 120.h),
      child: GridView.builder(
        shrinkWrap: true,
        itemCount: nodes.length,
        physics: NeverScrollableScrollPhysics(),
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 3,
          mainAxisSpacing: 10,
          crossAxisSpacing: 0,
          childAspectRatio: 150 / 40,
        ),
        itemBuilder: (context, index) {
          var child = Text(
            nodes[index].informName,
            style: style,
          );
          if ((index + 1) % 3 == 0) {
            return Container(
              alignment: AlignmentDirectional.center,
              child: child,
            );
          } else {
            return Container(
              alignment: AlignmentDirectional.center,
              decoration: BoxDecoration(border: Border(right: BorderSide(width: 2.w, color: Color(0xFFB3B3B3)))),
              child: child,
            );
          }
        },
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    var controller = Get.find<FlowChartController>();
    return Scaffold(
      resizeToAvoidBottomInset: false,
      backgroundColor: Colours.bg_color,
      appBar: MyAppBar(
        centerTitle: '流程图查看',
        titleColor: Colors.black,
      ),
      body: SingleChildScrollView(
        child: Container(
          width: MediaQuery.of(context).size.width,
          child: Column(
            children: [
              Padding(
                padding: EdgeInsets.only(top: 40.h),
                child: Text(
                  controller.processType.typeName,
                  style: TextStyle(color: Color(0xFF333333), fontSize: 30.sp, fontWeight: FontWeight.w400),
                ),
              ),
              SizedBox(height: 40.h),
              ..._buildGraph(controller.processNodeList),
              SizedBox(height: 70.h),
              _buildInfo(controller.processInformList),
              SizedBox(height: 80.h),
            ],
          ),
        ),
      ),
    );
  }
}
